﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BISPOS.Repository
{
    public class ProductRepository
    {
        BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
        public Price AddSpecialPrice(int productId, decimal amount)
        {
            Price specialPrice = new Price
            {
                Amount = amount,
                EndDate = DateTime.Now,
                IsNormal = false,
                Note = "",
                ProductId = productId,
                StartDate = DateTime.Now
            };
            db.Prices.InsertOnSubmit(specialPrice);
            db.SubmitChanges();
            return specialPrice;
        }
        public IEnumerable<ProductGroup> AllProductGroups() { return db.ProductGroups; }
        public ProductGroup GetProductGroup(int id) { return db.ProductGroups.FirstOrDefault(x => x.Id == id); }
        public Product Get(int id) { return db.Products.First(x => x.Id == id); }
        public Product Get(string barcode) { return db.Products.FirstOrDefault(x => x.Barcode == barcode); }
        public IEnumerable<Product> AllProducts() { return db.Products; }
        public void Save() { db.SubmitChanges(); }
        public IEnumerable<ProductGroup> SoldProductGroup(DateTime fromDate, DateTime toDate)
        {
            List<ProductGroup> groups = new List<ProductGroup>();
            foreach (var group in AllProductGroups())
            {
                IEnumerable<Product> products = group.SoldProducts(group.Id, fromDate, toDate);
                if (products.Count() > 0)
                {
                    groups.Add(group);
                }
            }
            return groups.AsEnumerable();
        }
        public void EditProduct(string barcode, string name, string unit, int productGroupId,bool isTaxed)
        {
            Product product = Get(barcode);
            product.Name = name;
            product.Unit = unit;
            product.ProductGroupId = productGroupId;
            product.IsTaxed = isTaxed;
            Save();
            
        }
        public void AddPrice(decimal amount, int productId, DateTime startDate, DateTime? endDate, string note, bool isNormal)
        {
            if (endDate.HasValue)
            {
                db.Prices.InsertOnSubmit(new Price
                {
                    Amount = amount,
                    ProductId = productId,
                    StartDate = startDate,
                    EndDate = endDate,
                    Note = note,
                    IsNormal = isNormal
                });
            }
            else
            {
                db.Prices.InsertOnSubmit(new Price
                {
                    Amount = amount,
                    ProductId = productId,
                    StartDate = startDate,
                    Note = note,
                    IsNormal = isNormal
                });
            }
            Save();
        }
    }
}
